package com.mfangsoft.zhuangjialong.app.order.service.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mfangsoft.zhuangjialong.app.applogin.mapper.CustomerEntityMapper;
import com.mfangsoft.zhuangjialong.app.order.mapper.AppOrderMapper;
import com.mfangsoft.zhuangjialong.app.order.model.BaseOrder;
import com.mfangsoft.zhuangjialong.app.order.model.ObiResult;
import com.mfangsoft.zhuangjialong.app.order.model.OrderBill;
import com.mfangsoft.zhuangjialong.app.order.model.OrderBillDetail;
import com.mfangsoft.zhuangjialong.app.order.model.OrderGift;
import com.mfangsoft.zhuangjialong.app.order.model.OrderGiving;
import com.mfangsoft.zhuangjialong.app.order.model.OrderInfo;
import com.mfangsoft.zhuangjialong.app.order.model.OrderPromote;
import com.mfangsoft.zhuangjialong.app.order.model.PaymentDetail;
import com.mfangsoft.zhuangjialong.app.order.model.ProductInfo;
import com.mfangsoft.zhuangjialong.app.order.service.IAppOrderService;
import com.mfangsoft.zhuangjialong.app.weixin.mapper.WeiXinMapper;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.HttpRequest;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.integration.user.mapper.UserInfoEntityMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.BaseUserManageMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.SellerPointEntityMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.BaseUserManage;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.SellerPointEntity;

@Service
public class AppOrderServiceImpl implements IAppOrderService {

	private static final Logger LOGGER = LoggerFactory.getLogger(AppOrderServiceImpl.class);

	@Autowired
	private AppOrderMapper appOrderMapper;
	@Autowired
	private CustomerEntityMapper customerEntityMapper;
	@Autowired
	BaseUserManageMapper baseUserManageMapper;
	@Autowired
	private UserInfoEntityMapper userInfoEntityMapper;
	@Autowired
	private SellerPointEntityMapper sellerPointEntityMapper;
	@Autowired
	WeiXinMapper weiXinMapper;

	@Override
	public void addOrder(OrderInfo order) {
		String shopAddress = appOrderMapper.selectShopAddress(order.getShopId());
		if (shopAddress != null) {
			String[] split = shopAddress.split(",");
			if (split.length == 3) {
				order.setProvince(split[0]);
				order.setCity(split[1]);
				order.setDistrict(split[2]);
			}
		}
		order.setOrderState(1);
		// Map<String,Object> m=new HashMap<>();
		// m.put("shopId", order.getShopId());
		// m.put("customerId", order.getCustomerId());
		// Map<String,Object> seller =
		// appOrderMapper.selectSellerByCustomerId(m);
		// if(seller==null){
		// Map<String,Object> orderInfo=new HashMap<>();
		// orderInfo.put("shopId", order.getShopId());
		// orderInfo.put("sellerId", order.getTypistId());
		// orderInfo.put("customerId", order.getCustomerId());
		// appOrderMapper.updateCustomerSeller(orderInfo);
		// order.setSellerId(order.getTypistId());
		// }else{
		// order.setSellerId(Integer.valueOf(seller.get("sellerId").toString()));
		// }
		// Integer explainId =
		// appOrderMapper.selectExplainId(order.getShopId());
		// order.setExplainId(explainId);
		List<ProductInfo> productInfoList = order.getProductInfoList();
		// 获取产品列表第一条数据的型号
		if (productInfoList != null && productInfoList.size() > 0) {
			order.setProductModel(productInfoList.get(0).getProductModel());
		}

		// 添加订单
		appOrderMapper.addOrder(order);
		if (order.getSellerId() != null) {
			SellerPointEntity spe = new SellerPointEntity();
			Integer sellerOrderPoint = appOrderMapper.selectSellerOrderPoint(order.getSellerId());
			if (sellerOrderPoint == 0) {
				spe.setPoint(30);
			} else {
				spe.setPoint(15);
			}
			spe.setSeller_id(Long.valueOf(order.getSellerId()));
			spe.setTitle(order.getOrderCode());
			spe.setType_index(2);
			spe.setType_name("订单");
			sellerPointEntityMapper.insertSelective(spe);
		}
		// if(seller==null){
		// spe.setSeller_id(Long.valueOf(order.getTypistId().toString()));
		// }else{
		// spe.setSeller_id(Long.valueOf(seller.get("sellerId").toString()));
		// }
		// 遍历添加产品信息
		if (productInfoList != null && productInfoList.size() > 0) {
			for (ProductInfo product : productInfoList) {
				product.setOrderId(order.getOrderId());
				appOrderMapper.addOrderProducts(product);
			}
		}
		// 添加活动订单活动券
		List<Map<String, Object>> num = order.getNum();
		if (num != null && num.size() > 0) {
			for (Map<String, Object> map : num) {
				Integer itemId = map.get("itemId") == null || "".equals(map.get("itemId")) ? 0
						: Integer.valueOf(map.get("itemId").toString());
				Integer num1 = map.get("num") == null || "".equals(map.get("num")) ? 0
						: Integer.valueOf(map.get("num").toString());
				if (num1 > 0) {
					for (int x = 0; x < num1; x++) {
						OrderGift orderGift = new OrderGift();
						String luckyCode = createCode();
						orderGift.setActivityId(order.getActivityId());
						orderGift.setCode(luckyCode);
						orderGift.setCustomerId(order.getCustomerId());
						orderGift.setStatus("0");
						orderGift.setItemId(itemId);
						orderGift.setOrderId(order.getOrderId());
						appOrderMapper.addOrderGift(orderGift);
					}
				}
			}
		}
		List<Map<String, Object>> presentRule = appOrderMapper.selectActivityPresentRule(order.getActivityId());
		if (presentRule != null && presentRule.size() > 0) {
			for (Map<String, Object> map : presentRule) {
				if (Double.valueOf(map.get("useMoney") == null || "".equals(map.get("useMoney")) ? "0"
						: map.get("useMoney").toString()) < Double.valueOf(order.getDepositPrice())) {
					appOrderMapper.updateGiftNum(map.get("gift") == null || "".equals(map.get("gift")) ? 0
							: Integer.valueOf(map.get("gift").toString()));
					appOrderMapper.updateActivityGiftNum(order.getActivityId());
				}
			}
		}
		List<OrderGiving> orderGiving = order.getOrderGiving();
		if (orderGiving != null && orderGiving.size() > 0) {
			for (OrderGiving o : orderGiving) {
				o.setOrderId(order.getOrderId());
				appOrderMapper.addOrderGiving(o);
			}
		}
		Map<String, Object> m = new HashMap<>();
		m.put("address", order.getConsigneeAddress());
		m.put("customerId", order.getCustomerId());
		weiXinMapper.updateCustomer(m);
	}

	/**
	 * 查询订单详情
	 */
	@Override
	public OrderInfo selectOrderDetail(Integer orderId) {
		// 查询订单详情
		OrderInfo orderDetail = appOrderMapper.selectOrderDetail(orderId);
		if (orderDetail != null) {
			List<ProductInfo> selectOrderProduct = appOrderMapper.selectOrderProduct(orderId);
			orderDetail.setProductInfoList(selectOrderProduct);
			String selectFiliale = appOrderMapper.selectFiliale(orderDetail.getShopId());
			orderDetail.setFiliale(selectFiliale);
			List<Map<String, Object>> orderGift = appOrderMapper.selectOrderGift(orderId);
			StringBuffer prizes = new StringBuffer();
			List<String> list = new ArrayList<String>();
			if (orderGift != null && orderGift.size() > 0) {
				for (Map<String, Object> map : orderGift) {
					String string = (String) map.get("gift");
					if (string != null) {
						String[] split = string.split(",");
						for (int x = 0; x < split.length; x++) {
							list.add(split[x]);
						}
					}
				}
			}
			if (list != null && list.size() > 0) {
				for (int y = 0; y < list.size(); y++) {
					if (y == 0) {
						if ("".equals(list.get(y))) {

						} else {
							prizes.append(list.get(y));
						}
					} else {
						if (prizes.length() > 0) {
							prizes.append("/" + list.get(y));
						} else {
							prizes.append(list.get(y));
						}
					}
				}
			}
			orderDetail.setActivityDetail(orderGift);
			orderDetail.setPrizes(prizes.toString());
			// 查询活动次数规则
			// List<Map<String,Object>> activityNumRule =
			// appOrderMapper.selectActivityNumRule(orderDetail.getActivityId());
			// 查询满赠规则
			// List<Map<String,Object>> activityPresentRule =
			// appOrderMapper.selectActivityPresentRule(orderDetail.getActivityId());
			// orderDetail.setActivityNumRule(activityNumRule);
			// orderDetail.setActivityPresentRule(activityPresentRule);
			List<String> orderGiving = appOrderMapper.selectOrderGiving(orderId);
			if (orderGiving != null && orderGiving.size() > 0) {
				StringBuffer sb = new StringBuffer();
				for (String s : orderGiving) {
					if (sb.length() > 0) {
						sb.append("/" + s);
					} else {
						sb.append(s);
					}
				}
				orderDetail.setPrize(sb.toString());
			}
		}
		return orderDetail;
	}

	/**
	 * 修改订单
	 */
	@Override
	public void updateOrder(Map<String, Object> map) {
		if (map != null) {
			OrderInfo order = new OrderInfo();
			order.setOrderId(Integer.valueOf(map.get("orderId").toString()));
			order.setRemarks(map.get("remarks").toString());
			// order.setIsUseActivityCard(map.get("isUseActivityCard")==null?.toString());
			appOrderMapper.updateOrder(order);
		}
	}

	/**
	 * 修改订单
	 * 
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	@Override
	public void updateRefreshOrder(Map<String, Object> map) throws Exception {
		String string = HttpRequest.sendGet("http://mos.powerdekor.com.cn:8011/qobinf.ashx",
				"type=GOI01&sn=qobi01.powerdekor.com.cn&userMob=" + "&orderBillNo=" + map.get("orderCode"));
		ObjectMapper objectMapper = new ObjectMapper();
		ObiResult result = objectMapper.readValue(string, ObiResult.class);
		OrderInfo order = new OrderInfo();
		order.setOrderId(Integer.valueOf(map.get("orderId").toString()));
		Map<String, Object> activityInfo = appOrderMapper
				.selectActivityInfo(Integer.valueOf(map.get("activityId").toString()));
		if (result != null) {
			OrderBill data = result.getData();
			if (data != null) {
				order.setConsignee(data.getCustName());
				order.setConsigneePhone(data.getCustMob());
				order.setConsigneeAddress(data.getCustAddr());
				List<OrderBillDetail> orderbilldetails = data.getOrderbilldetails();
				// 删除订单产品
				appOrderMapper.deleteOrderProduct(Integer.valueOf(map.get("orderId").toString()));
				if (orderbilldetails != null && orderbilldetails.size() > 0) {
					double orderPrice = 0l;
					for (OrderBillDetail products : orderbilldetails) {
						double price = Double.valueOf(products.getfPrice());
						double num = Double.valueOf(products.getfNum());
						ProductInfo product = new ProductInfo();
						product.setProductModel(products.getfProductCode());
						product.setPrice(products.getfPrice());
						product.setProductName(products.getfProductName());
						product.setTotalPrice("" + price * num);
						product.setNum(new Double(num).intValue());
						product.setOrderId(Integer.valueOf(map.get("orderId").toString()));
						orderPrice += price * num;
						appOrderMapper.addOrderProducts(product);
					}
					order.setOrderPrice("" + orderPrice);
				}

				Long endTime = Long.valueOf(activityInfo.get("endTime").toString());
				if (endTime > (new Date().getTime() / 1000)) {
					List<PaymentDetail> paymenntDetails = data.getPaymenntDetails();
					if (paymenntDetails != null && paymenntDetails.size() > 0) {
						double realPayPrice = 0;
						double depositPrice = 0;
						order.setPayStatus(paymenntDetails.get(0).getfPayType());
						for (PaymentDetail pay : paymenntDetails) {
							if (activityInfo != null) {
								Long startTime = Long.valueOf(activityInfo.get("startTime1").toString());
								// Long endTime =
								// Long.valueOf(activityInfo.get("endTime").toString());
								String receivedDate = pay.getfReceivedDate();
								if (!"".equals(receivedDate)) {
									SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
									Date parse = f.parse(receivedDate);
									if ((parse.getTime() / 1000) > startTime && (parse.getTime() / 1000) < endTime) {
										depositPrice += Double.valueOf(pay.getfReceivedAmount());
									}
								}
							}
							realPayPrice += Double.valueOf(pay.getfReceivedAmount());
						}
						order.setRealPayPrice("" + realPayPrice);
						order.setDepositPrice("" + depositPrice);
						Map<String, Object> activityNum = new HashMap<>();
						activityNum.put("activityId", map.get("activityId"));
						activityNum.put("money", depositPrice);
						List<Map<String, Object>> num = appOrderMapper.selectActivityNum(activityNum);
						if (num != null && num.size() > 0) {
							for (Map<String, Object> m : num) {
								Integer itemId = Integer.valueOf(m.get("itemId") == null || "".equals(m.get("itemId"))
										? "0" : m.get("itemId").toString());
								Integer num1 = Integer.valueOf(m.get("num") == null || "".equals(m.get("num")) ? "0"
										: m.get("num").toString());
								OrderGift orderGift = new OrderGift();
								orderGift.setItemId(itemId);
								orderGift.setOrderId(order.getOrderId());
								Integer selectNum = appOrderMapper.selectNum(orderGift);
								for (int x = 0; x < num1 - selectNum; x++) {
									OrderGift orderGift1 = new OrderGift();
									String luckyCode = createCode();
									orderGift1.setActivityId(Integer.valueOf(map.get("activityId").toString()));
									orderGift1.setCode(luckyCode);
									orderGift1.setCustomerId(Integer.valueOf(map.get("customerId").toString()));
									orderGift1.setStatus("0");
									orderGift1.setItemId(itemId);
									orderGift1.setOrderId(order.getOrderId());
									appOrderMapper.addOrderGift(orderGift1);
								}
							}
						}

						List<Map<String, Object>> presentRule = appOrderMapper
								.selectActivityPresentRule(Integer.valueOf(map.get("activityId").toString()));
						appOrderMapper.deleteOrderGiving(Integer.valueOf(map.get("orderId").toString()));
						if (presentRule != null && presentRule.size() > 0) {
							for (int x = 0; x < presentRule.size(); x++) {
								if (Double.valueOf(presentRule.get(x).get("useMoney").toString()) < depositPrice) {
									String gift = appOrderMapper
											.selectGiftById(Integer.valueOf(presentRule.get(x).get("gift").toString()));
									OrderGiving orderGiving = new OrderGiving();
									orderGiving.setActivityId(Integer.valueOf(map.get("activityId").toString()));
									orderGiving.setGift(gift);
									orderGiving.setGiftId(Integer.valueOf(presentRule.get(x).get("gift").toString()));
									orderGiving.setOrderId(Integer.valueOf(map.get("orderId").toString()));
									appOrderMapper.addOrderGiving(orderGiving);
								}
							}
						}
					}
				}
			}
		}
		appOrderMapper.updateOrder(order);

		/*
		 * //修改订单 appOrderMapper.updateOrder(order); //删除订单产品
		 * appOrderMapper.deleteOrderProduct(order.getOrderId()); //遍历产品列表进行添加
		 * List<ProductInfo> productInfoList = order.getProductInfoList(); if
		 * (productInfoList != null && productInfoList.size() > 0) { for
		 * (ProductInfo product : productInfoList) {
		 * product.setOrderId(order.getOrderId());
		 * appOrderMapper.addOrderProducts(product); } } //获取活动券数量 将新添加的抽奖次数添加进去
		 * List<Map<String,Object>> num = order.getNum(); if(num!=null &&
		 * num.size()>0){ for(Map<String,Object> map:num){ Integer itemId =
		 * Integer.valueOf(map.get("itemId").toString()); Integer num1 =
		 * Integer.valueOf(map.get("num").toString()); OrderGift orderGift=new
		 * OrderGift(); orderGift.setItemId(itemId);
		 * orderGift.setOrderId(order.getOrderId()); Integer selectNum =
		 * appOrderMapper.selectNum(orderGift); for(int
		 * x=0;x<num1-selectNum;x++){ OrderGift orderGift1=new OrderGift();
		 * String luckyDate = new
		 * SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()); String
		 * luckyCode = "HDQ" + luckyDate +
		 * String.valueOf(Math.round(Math.random() * 1000));
		 * orderGift1.setActivityId(order.getActivityId());
		 * orderGift1.setCode(luckyCode);
		 * orderGift1.setCustomerId(order.getCustomerId());
		 * orderGift1.setStatus(0); orderGift1.setItemId(itemId);
		 * appOrderMapper.addOrderGift(orderGift1); } } }
		 */
	}

	/**
	 * 查询导购订单列表
	 */
	@Override
	public Page<OrderInfo> selectSellerOrderPage(Page<OrderInfo> page) {
		List<OrderInfo> orders = appOrderMapper.selectSellerOrderPage(page);
		page.setData(orders);
		return page;
	}

	/**
	 * 查询消费者订单列表
	 */
	@Override
	public Page<OrderInfo> selectCustomerOrderPage(Page<OrderInfo> page) {
		List<OrderInfo> orders = appOrderMapper.selectCustomerOrderPage(page);
		page.setData(orders);
		return page;
	}

	@Override
	public Page<OrderPromote> selectOrdersByUserManagerId(Page<OrderPromote> page) {
		Map<String, Object> map = (Map<String, Object>) page.getParam();
		// String typistName = map.get("typistName").toString();
		// if(typistName != null){
		// List<Integer> userManagerIds =
		// baseUserManageMapper.selectUserManagersByUName(typistName);
		// map.put("userManagerIds", userManagerIds);
		// page.setParam(map);
		// }
		List<OrderPromote> orderPromoteList = new ArrayList<OrderPromote>();
		List<BaseOrder> baseOrderList = appOrderMapper.selectOrderByUserManagerIdForPage(page);
		Integer exshopper_id = Integer.parseInt(map.get("exshopper_id").toString());
		BaseUserManage baseUserManage = baseUserManageMapper.selectByPrimaryKey(exshopper_id);
		for (BaseOrder baseOrder : baseOrderList) {
			OrderPromote orderPromote = new OrderPromote();
			orderPromote.setId(baseOrder.getId());
			orderPromote.setOrder_code(baseOrder.getOrder_code());
			orderPromote.setCreate_time(baseOrder.getCreate_time());
			orderPromote.setOrder_state(baseOrder.getOrder_state());
			orderPromote.setOrder_type(baseOrder.getOrder_type());
			orderPromote.setProduct_model(baseOrder.getProduct_model());
			orderPromote.setCustomerName(
					customerEntityMapper.selectCustomerByCustomer_id(baseOrder.getCustomer_id().longValue()) == null
							? ""
							: customerEntityMapper.selectCustomerByCustomer_id(baseOrder.getCustomer_id().longValue())
									.getName());
			orderPromote.setTypistName(baseUserManage.getName());
			orderPromote.setOrder_price(baseOrder.getOrder_price());
			orderPromote.setUserManager_name(
					userInfoEntityMapper.selectUserBySysUserId(baseOrder.getShop_id().longValue()) == null ? ""
							: userInfoEntityMapper.selectUserBySysUserId(baseOrder.getShop_id().longValue()).getName());
			orderPromoteList.add(orderPromote);
		}
		page.setData(orderPromoteList);
		return page;
	}

	/**
	 * 助手关闭订单
	 */
	@Override
	public void updateOrderStatus(OrderInfo order) {
		order.setCloseWay(2);
		appOrderMapper.updateOrderStatus(order);
	}

	@Override
	public ResponseMessage<Map<String, Object>> checkOrder(Map<String, Object> map) throws Exception {
		ResponseMessage<Map<String, Object>> message = new ResponseMessage<Map<String, Object>>();
		Integer integer = appOrderMapper.selectOrderByOrderCode(map.get("orderCode") + "");
		if (integer > 0) {
			message.setCode("2");
			message.setMessage("订单编号已存在！");
			return message;
		}
		Map<String, Object> orderInfo = new HashMap<>();
		String string = HttpRequest.sendGet("http://mos.powerdekor.com.cn:8011/qobinf.ashx",
				"type=GOI01&sn=qobi01.powerdekor.com.cn&userMob=" + map.get("customerPhone") + "&orderBillNo="
						+ map.get("orderCode"));
		ObjectMapper objectMapper = new ObjectMapper();
		ObiResult result = objectMapper.readValue(string, ObiResult.class);
		if (result == null || result.getData() == null) {
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		}
		Map<String, Object> activityInfo = appOrderMapper
				.selectActivityInfo(Integer.valueOf(map.get("activityId").toString()));
		orderInfo.put("activityCode", activityInfo == null ? "" : activityInfo.get("activity_code"));
		orderInfo.put("activityName", activityInfo == null ? "" : activityInfo.get("activityName"));
		orderInfo.put("activityId", map.get("activityId"));
		orderInfo.put("orderCode", map.get("orderCode"));
		if (result != null && result.getData() != null) {
			OrderBill data = result.getData();
			orderInfo.put("consignee", data.getCustName());
			orderInfo.put("consigneePhone", data.getCustMob());
			orderInfo.put("consigneeAddress", data.getCustAddr());
			orderInfo.put("createTime", data.getCreateTime());
			List<OrderBillDetail> orderbilldetails = data.getOrderbilldetails();
			if (orderbilldetails != null && orderbilldetails.size() > 0) {
				List<Map<String, Object>> productInfoList = new ArrayList<>();
				double orderPrice = 0l;
				for (OrderBillDetail products : orderbilldetails) {
					Map<String, Object> product = new HashMap<>();
					product.put("productModel", products.getfProductCode());
					product.put("productName", products.getfProductName());
					double price = Double.valueOf(products.getfPrice());
					double num = Double
							.valueOf("".equals(products.getfNum().toString()) ? "0" : products.getfNum().toString());
					product.put("price", price);
					product.put("imageUrl", null);
					product.put("num", num);
					product.put("totalPrice", price * num);
					productInfoList.add(product);
					orderPrice += price * num;
				}
				orderInfo.put("orderPrice", orderPrice);
				orderInfo.put("productInfoList", productInfoList);
			}
			List<PaymentDetail> paymenntDetails = data.getPaymenntDetails();
			if (paymenntDetails != null && paymenntDetails.size() > 0) {
				double realPayPrice = 0l;
				double depositPrice = 0l;
				orderInfo.put("payStatus", paymenntDetails.get(0).getfPayType());
				for (PaymentDetail pay : paymenntDetails) {
					if (activityInfo != null) {
						Long startTime = Long.valueOf(activityInfo.get("startTime1").toString());
						Long endTime = Long.valueOf(activityInfo.get("endTime").toString());
						String receivedDate = pay.getfReceivedDate();
						if (!"".equals(receivedDate)) {
							SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
							Date parse = f.parse(receivedDate);
							if ((parse.getTime() / 1000) > startTime && (parse.getTime() / 1000) < endTime) {
								depositPrice += Double.valueOf(pay.getfReceivedAmount());
							}
						}
					}
					realPayPrice += Double.valueOf(pay.getfReceivedAmount());
				}
				orderInfo.put("realPayPrice", realPayPrice);
				orderInfo.put("depositPrice", depositPrice);
				Map<String, Object> activityNum = new HashMap<>();
				activityNum.put("activityId", map.get("activityId"));
				activityNum.put("money", depositPrice);
				List<Map<String, Object>> num = appOrderMapper.selectActivityNum(activityNum);
				orderInfo.put("num", num);
				List<Map<String, Object>> presentRule = appOrderMapper
						.selectActivityPresentRule(Integer.valueOf(map.get("activityId").toString()));
				if (presentRule != null && presentRule.size() > 0) {
					StringBuffer prize = new StringBuffer();
					List<Map<String, Object>> prizes = new ArrayList<>();
					for (int x = 0; x < presentRule.size(); x++) {
						if (Double.valueOf(presentRule.get(x).get("useMoney") == null ? "0"
								: presentRule.get(x).get("useMoney").toString()) < depositPrice) {
							if (prize.length() > 0) {
								if (presentRule.get(x).get("gift") != null
										&& !"".equals(presentRule.get(x).get("gift").toString())) {
									String gift = appOrderMapper
											.selectGiftById(Integer.valueOf(presentRule.get(x).get("gift").toString()));

									prize.append("/" + gift);
									Map<String, Object> m = new HashMap<>();
									m.put("activityId", map.get("activityId"));
									m.put("giftId", presentRule.get(x).get("gift"));
									m.put("gift", gift);
									prizes.add(m);
								}
							} else {
								if (presentRule.get(x).get("gift") != null
										&& !"".equals(presentRule.get(x).get("gift").toString())) {
									String gift = appOrderMapper
											.selectGiftById(Integer.valueOf(presentRule.get(x).get("gift").toString()));
									prize.append(gift);
									Map<String, Object> m = new HashMap<>();
									m.put("activityId", map.get("activityId"));
									m.put("giftId", presentRule.get(x).get("gift"));
									m.put("gift", gift);
									prizes.add(m);
								}
							}
						}
					}
					orderInfo.put("prize", prize);
					orderInfo.put("orderGiving", prizes);
				}
			}
		}
		Map<String, Object> explain = appOrderMapper
				.selectShopExplain(Integer.valueOf(activityInfo.get("user_id").toString()));
		if (explain != null) {
			orderInfo.put("explain", explain.get("activity_explain"));
			orderInfo.put("explainId", explain.get("explainId"));
		} else {
			orderInfo.put("explain", "");
			orderInfo.put("explainId", 0);
		}
		Map<String, Object> m = appOrderMapper.selectSellerByCustomerId(map);
		if (m == null) {
			orderInfo.put("sellerName", null);
			orderInfo.put("sellerPhone", null);
			orderInfo.put("sellerId", null);
		} else {
			orderInfo.put("sellerName", m.get("sellerName"));
			orderInfo.put("sellerPhone", m.get("sellerPhone"));
			orderInfo.put("sellerId", m.get("sellerId"));
		}
		message.setResult(orderInfo);
		message.setCode(SysConstant.SUCCESS_CODE);
		message.setMessage(SysConstant.SUCCESS_MSG);
		return message;
	}

	@Override
	public ResponseMessage<Map<String, Object>> createOrder(Map<String, Object> map) throws Exception {
		ResponseMessage<Map<String, Object>> message = new ResponseMessage<Map<String, Object>>();
		Integer integer = appOrderMapper.selectOrderByOrderCode(map.get("orderCode").toString());
		if (integer > 0) {
			message.setCode("2");
			message.setMessage("订单编号已存在！");
			return message;
		}
		String string = HttpRequest.sendGet("http://mos.powerdekor.com.cn:8011/qobinf.ashx",
				"type=GOI01&sn=qobi01.powerdekor.com.cn&userMob=" + map.get("customerPhone") + "&orderBillNo="
						+ map.get("orderCode"));
		ObjectMapper objectMapper = new ObjectMapper();
		ObiResult result = objectMapper.readValue(string, ObiResult.class);
		if (result != null) {
			OrderBill orderBill = result.getData();
			if (orderBill != null) {
				String orderBillNo = orderBill.getOrderBillNo();
				String custName = orderBill.getCustName();
				String custMob = orderBill.getCustMob();
				Map<String, Object> m = new HashMap<>();
				m.put("orderBillNo", orderBillNo);
				m.put("custName", custName);
				m.put("custMob", custMob);
				message.setResult(m);
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				return message;
			} else {
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				return message;
			}
		} else {
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		}
	}

	public String createCode() {
		String s = "" + new Date().getTime();
		String code = "HDQ" + s.substring(s.length() - 3, s.length()) + Math.round((Math.random() * 9 + 1) * 1000000);
		Integer giftCode = appOrderMapper.selectGiftCode(code);
		if (giftCode > 0) {
			return createCode();
		} else {
			return code;
		}
	}

	@Override
	public ResponseMessage<List<Map<String, Object>>> selectCustomerActivity(Map<String, Object> map) {
		ResponseMessage<List<Map<String, Object>>> message = new ResponseMessage<List<Map<String, Object>>>();
		List<Map<String, Object>> list = appOrderMapper.selectCustomerActivity(map);
		if (list != null && list.size() > 0) {
			message.setResult(list);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} else {
			message.setCode("2");
			message.setMessage("没有参加任何活动");
			return message;
		}
	}

}
